/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.merak.core.model;

import java.io.Serializable;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

import org.merak.core.annotation.Skip;
import org.merak.core.annotation.Style;
import org.merak.core.text.ObjectJSON;
import org.merak.core.text.ObjectNVP;
import org.merak.core.text.Textifiable;

/**
 * Copyright © 2010-2012 Merak Computing. All rights reserved.<br><br>
 *
 * This class is supposed to be extended by all identifiable objects. Basically, it holds only a
 * single attribute: the id, which has the properties of being comparable and theoretically unique
 * inside a scope. This class alone cannot ensure the scope uniqueness, but it forbids
 * Id update operations. Even so, there is a setId(T id) method that can be called only once,
 * otherwise it will throw a runtime IdentifierException. Of course, this exception
 * will be thrown also if id has already been set by constructor.
 *
 * @author fmarinho.
 * @version 1.00.00 - Jul 22, 2012 - fmarinho - initial version.
 *
 * @since MerakCore 1.00.00
 */
@MappedSuperclass
public abstract class Identified<T extends Serializable & Comparable<T>>
           implements Identifiable<T>,Textifiable
{

	//~ Attributes ////////////////////O////////////////////////////////////////////
	//*****************************************************************************
	/** The identifier: a symbol that establishes the identity of the one bearing it */
	private T id;

	//~ Constructors //////////////////////////////////////////////////////////////
	//*****************************************************************************
	/** Obliges to initialize the Id, even if it is null. It can be called only
	 * by subclasses, to reinforce the abstract nature of this class */
	protected Identified(T id) {

		this.setId(id);

	}

	//~ Methods ///////////////////////////////////////////////////////////////////
	//*****************************************************************************
	public void setId(T id) {

		/* Id update is illegal... An update occurs when this.id:
		 * 1. is already set and
		 * 2. is different of provided id
		 */
		if ( this.id!=null && !this.id.equals(id) ) throw new IdentifierException(
			"Identifier update is illegal: the object " + this +
			" received an id update to " + id
		);

		// Set id since all goes right
		this.id = id;

	}
	//*****************************************************************************
	@Override
	@Id @GeneratedValue(generator="ID") @Style(zindex=0)
	public T getId() {

		return this.id;

	}
	//*****************************************************************************
	/* (non-Javadoc)
	 * @see org.merak.core.model.Identifiable#hasId(java.io.Serializable)
	 */
	@Override
	public boolean hasId(T value) {

		return this.getId() == value       // if both are null or same instance
			|| this.getId().equals(value); // if both are not same instance

	}
	//*****************************************************************************
	/* (non-Javadoc)
	 * @see org.merak.core.model.Identifiable#anonymous()
	 */
	@Override @Transient @Skip
	public boolean anonymous() {

		return this.id == null;

	}
	//*****************************************************************************
	/* (non-Javadoc)
	 * @see org.merak.core.model.Identifiable#selfsame(org.merak.core.model.Identifiable)
	 */
	@Override
	public boolean selfsame(Identifiable<T> object) {

		// Null is never selfsame to instanced objects
		if (object == null) return false;

		/* It does not make sense to say objects are the same entity or not
		 * while identification is pending. The id must be provided before selfsameness comparison,
		 * or it is certainly a bug in your code.
		 * TODO think about exception "Anonymous objects cannot be compared for selfsameness"
		 */
		if (this.anonymous() || object.anonymous()) {
			return false;
		}

		// Objects must pertain to same classes and have same id
		return this.getClass().isInstance(object)
			&& this.getId().equals(object.getId());

	}
	//*****************************************************************************
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	@SuppressWarnings("unchecked")
	public boolean equals(Object object) {

		// Null is never equals to instanced objects
		if (object == null) return false;

		// If objects pertain to different classes, they are different
		if (!this.getClass().isInstance(object)) return false;

		// Here, it is safe to say both objects are identifiable
		Identifiable<T> other = (Identifiable<T>) object;

		/* The unique possible assert at this point to say objects are equal,
		 * is to check if both objects have same id (selfsame) or null id (anonymous)
		 */
		return this.getId() == other.getId()        // if both are null or same instance
			|| this.getId().equals(other.getId());  // if both are not same instance

	}
	//*****************************************************************************
	@Override
	public int hashCode() {

		// The case when object has not been identified yet
		if (this.anonymous()) return 0;

		// The case when object has already been identified
		return this.getId().hashCode();

	}
	//****************************************************************************
	@Override
	public String toJSON() {

		return ObjectJSON.toJson(this);

	}
	//****************************************************************************
	@Override
	public String toNVP() {

		return ObjectNVP.toNVP(this);

	}
	//*****************************************************************************
	@Override
	public String toString() {

		return this.getClass().getSimpleName() + "#" + this.getId();

	}
	//*****************************************************************************

}